<template>
  <div id="detail">
    <detail-nar-bar class="detail-nav" ref="nav" @titleClick="titleClick"/>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info ref="param" :param-info="paramInfo" />
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommend" :goods="recommends"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
    <detail-bottom-bar @addCart="addToCart"/>
    <toast/>
  </div>
</template>

<script>
import DetailNarBar from "./childrenComps/DetailNarBar";
import DetailSwiper from "./childrenComps/DetailSwiper";
import DetailBaseInfo from "./childrenComps/DetailBaseInfo";
import DetailShopInfo from "./childrenComps/DetailShopInfo";
import DetailGoodsInfo from "./childrenComps/DetailGoodsInfo";
import DetailParamInfo from "./childrenComps/DetailParamInfo";
import DetailCommentInfo from "./childrenComps/DetailCommentInfo";
import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";
import DetailBottomBar from "./childrenComps/DetailBottomBar";
import Toast from "components/common/toast/Toast";
import {debounce} from "common/utils";
import {itemListenerMixin, backTopMixin} from "common/mixin";
import { getDetail, getRecommend, Goods, Shop, DetailInfo, GoodsParam } from "network/detail";
import {mapActions} from "vuex"


export default {
  name: "Detail",
  components: {
   DetailNarBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    Toast
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0
    }
  },
  mixins: [itemListenerMixin,backTopMixin],
  methods: {
    ...mapActions({
      addCart: 'addCart'
    }),
    imageLoad() {
      this.newRefresh()
      this.getThemeTopY()
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    contentScroll(position) {
      this.listenShowBackTop(position)
      const positionY = -position.y
      let length = this.themeTopYs.length
      for (let i = 0; i < length; i++) {
        if (this.currentIndex != i && (
          (i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) ||
          (i === length - 1 && positionY >= this.themeTopYs[i]))) {
          this.currentIndex = i
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }
    },
    addToCart() {
      const product = {}
      product.image = this.topImages[0];
      product.desc = this.goods.desc;
      product.title = this.goods.title;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      // this.$store.dispatch('addCart',product)
      this.addCart(product).then(res => {
        //原始 toast 方法的设计
        // this.isShow = true
        // this.message = res
        // setTimeout(() => {
        //   this.isShow = false
        //   this.message = ''
        // },1500)

        this.$toast.show(res)
      })
    }
  },
    mounted() {
      // let newRefresh = debounce(this.$refs.scroll.refresh)
      // this.itemImgListener = () => {
      //   newRefresh()
      // }
      // this.$bus.$on('itemImageLoad',this.itemImgListener)
    }
    ,
    created() {
      //保存存入的iid
      this.iid = this.$route.params.iid
      //根据iid请求详情数据
      getDetail(this.iid).then(res => {
        console.log(res)
        const data = res.data.result;
        this.topImages = data.itemInfo.topImages
        //获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        //创建店铺信息的对象
        this.shop = new Shop(data.shopInfo)
        //保存商品详情数据
        this.detailInfo = new DetailInfo(data.detailInfo);
        //获取参数的信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
        //取出评论信息
        if (data.rate.cRate != 0) {
          this.commentInfo = data.rate.list[0]
        }
      })
      //请求推荐数据
      getRecommend().then(res => {
        const data = res.data.data
        this.recommends = data.list
      })
      //给getThemeTopY赋值
      this.getThemeTopY = debounce(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.param.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        // console.log(this.themeTopYs)
      }, 50)
    },
    destroyed() {
      this.$bus.$on('itemImageLoad', this.itemImgListener)
    }
  }

</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
.content {
  height: calc(100% - 44px)
}
</style>
